/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mytv.backbean;

import com.mytv.entity.MovieUser;
import com.mytv.service.MovieUserService;
import java.text.DecimalFormat;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

    
/**
 *
 * @author Sirius
 */
@ManagedBean
@ViewScoped
public class RegisterUserBean extends BackBean {
    
    private MovieUser user;
    private String confirmPassword;
    private boolean errorRegister;
    private boolean samePassword;
    private boolean existUser;
    
    /**
     * Constructor of class
     */
    public RegisterUserBean() {
        user = new MovieUser();
        confirmPassword = "";
        errorRegister = false;
        samePassword = true;
    }

    /**
     * Get user service from spring context
     * @return A spring user service
     */
    public MovieUserService getUserService() {
        return (MovieUserService) getSpringBean(MovieUserService.class);
    }

    /**
     * Register new user
     */
    public void register() {
        errorRegister = false;
        existUser = false;
        if(getConfirmPassword().equals(getUser().getPassword())) {
            samePassword = true;
            if(getUser().getNickname() != null && !user.getNickname().isEmpty()) {
                if(!getUserService().isExistUserByNickName(getUser().getNickname())) {
                    int users = getUserService().countUsers() + 1;
                    DecimalFormat format = new DecimalFormat("000");
                    getUserService().save(format.format(users), user.getNickname(), encrypt(user.getPassword()));
                    MovieUser newUser = getUserService().getUser(user.getNickname(), encrypt(user.getPassword()));
                    getSessionBean().setUser(newUser);
                } else {
                    existUser = true;
                }
            } else {
                errorRegister = true;
            }
        } else {
            samePassword = false;
        }
        setConfirmPassword("");
        user.setPassword("");
    }

    /**
     * Get if get an error when register new user
     * @return <code>true</code>: If exist error register<br/><code>false</code>: Otherwise
     */
    public boolean isErrorRegister() {
        return errorRegister;
    }
    
    /**
     * Get if have same password
     * @return <code>true</code>: Is same password<br/><code>false</code>: Otherwise
     */
    public boolean isSamePassword() {
        return samePassword;
    }
    
    /**
     * Get if user exist
     * @return <code>true</code>: Is user exist already<br/><code>false</code>: Otherwise
     */
    public boolean isExistUser() {
        return existUser;
    }

    /**
     * @return the user
     */
    public MovieUser getUser() {
        return user;
    }

    /**
     * @return the confirmPassword
     */
    public String getConfirmPassword() {
        return confirmPassword;
    }

    /**
     * @param confirmPassword the confirmPassword to set
     */
    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }
        
}